package com.syh.jn.scm.service.impl.downstream.finance;

import cn.redscarf.commons.wrapper.Restrictions;
import cn.redscarf.commons.wrapper.Wrapper;
import com.alibaba.dubbo.config.annotation.Service;
import com.syh.core.ddd.event.annotation.DomainEventHandle;
import com.syh.jn.scm.domain.downstream.entity.finance.DownStatementAccountBillCart;
import com.syh.jn.scm.domain.downstream.repository.finance.DownStatementAccountBillCartReadRepository;
import com.syh.jn.scm.domain.downstream.repository.finance.DownStatementAccountBillCartRepository;
import com.syh.jn.scm.domain.supplier.entity.finance.StatementAccountBill;
import com.syh.jn.scm.domain.supplier.entity.finance.StatementAccountBillCart;
import com.syh.jn.scm.domain.supplier.event.finance.StatementAccountBillEvent;
import com.syh.jn.scm.domain.supplier.repository.finance.StatementAccountBillCartReadRepository;
import com.syh.jn.scm.domain.supplier.repository.finance.StatementAccountBillCartRepository;
import com.syh.jn.scm.dto.downstream.finance.downStatementAccountBillCart.DownStatementAccountBillCartDto;
import com.syh.jn.scm.dto.downstream.finance.downStatementAccountBillCart.DownStatementAccountBillCartVo;
import com.syh.jn.scm.dto.supplier.finance.statementAccountBillCart.StatementAccountBillCartDto;
import com.syh.jn.scm.dto.supplier.finance.statementAccountBillCart.StatementAccountBillCartVo;
import com.syh.jn.scm.service.api.downstream.finance.DownStatementAccountBillCartService;
import com.syh.jn.scm.service.api.supplier.finance.StatementAccountBillCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by soci.song on 2018/12/11.
 */
@Service
@Transactional
public class DownStatementAccountBillCartServiceImpl implements DownStatementAccountBillCartService {

  @Autowired
  private DownStatementAccountBillCartRepository statementAccountBillCartRepository;

  @Resource(name="downStatementAccountBillCartReadRepositoryMbImpl")
  private DownStatementAccountBillCartReadRepository statementAccountBillCartReadRepository;

  @Override
  public List<DownStatementAccountBillCartVo> findList(Wrapper wrapper) {
    //获取对账单缓存
    List<DownStatementAccountBillCartVo> statementAccountBillCartVos = this.statementAccountBillCartReadRepository.getList(wrapper);
    List<DownStatementAccountBillCartVo> returnVos = new ArrayList<>();
    //过滤对账单缓存
    statementAccountBillCartVos.forEach(vo-> {
      //如果该对账单缓存不能生成对账单 则删除该缓存
      if (Short.valueOf("0").equals(vo.getStatementState()) ) {
        Wrapper deleteWp = Wrapper.build().append(Restrictions.eq("sourceId",vo.getSourceId())).append(Restrictions.eq("sourceType",vo.getSourceType()));
        this.statementAccountBillCartRepository.remove(deleteWp);
      //否则把该缓存添加到返回前端的vo列表中
      } else {
        returnVos.add(vo);
      }
    });
    return returnVos;
  }

  @Override
  public void save(List<DownStatementAccountBillCartDto> statementAccountBillCartDtos, Wrapper wrapper) {
    List<DownStatementAccountBillCart> statementAccountBillCarts = new ArrayList<>();
    statementAccountBillCartDtos.forEach(dto -> {
      DownStatementAccountBillCart statementAccountBillCart = new DownStatementAccountBillCart(dto);
      statementAccountBillCarts.add(statementAccountBillCart);
    });
    this.statementAccountBillCartRepository.add(statementAccountBillCarts, wrapper);
  }


  /**
   * 消费者：监听对账单创建（删除相关缓存）
   * @param statementAccountBill
   */
  @DomainEventHandle(StatementAccountBillEvent.STATEMENT_BILL_ACCOUNT_CREATED)
  protected void handelStatementAccountBillCreated(StatementAccountBill statementAccountBill) {
    //删除该操作员关于该公司的对账缓存
    Wrapper wrapper = Wrapper.build().append(Restrictions.eq("customerId",statementAccountBill.getCustomerId())).append(Restrictions.eq("customerType",statementAccountBill.getCustomerType())).append(Restrictions.eq("brokerageId",statementAccountBill.getBrokerageId()));
    this.statementAccountBillCartRepository.remove(wrapper);
    //删除已生成对账的缓存
    statementAccountBill.getStatementAccountBillItems().forEach(item-> {
      Wrapper otherWrapper = Wrapper.build().append(Restrictions.eq("sourceId",item.getSourceId())).append(Restrictions.eq("sourceType",item.getSourceType()));
      this.statementAccountBillCartRepository.remove(otherWrapper);
    });
  }
}
